package com.b511.swpu.cementbackend.service.impl;

import com.b511.swpu.cementbackend.dto.output.PageOutputDto;
import com.b511.swpu.cementbackend.dto.output.ValidateInfoOutputDto;
import com.b511.swpu.cementbackend.entity.SYS_DATA_VALIDATE_RULE;
import com.b511.swpu.cementbackend.entity.Sys_Module_Data;
import com.b511.swpu.cementbackend.repository.SYS_Data_Validate_RuleRepository;
import com.b511.swpu.cementbackend.repository.WellDocRepository;
import com.b511.swpu.cementbackend.service.DataQueryService;
import com.b511.swpu.cementbackend.service.DataValidateService;
import com.b511.swpu.cementbackend.util.DBHelper;
import com.b511.swpu.cementbackend.util.R;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class DataValidateServiceImpl implements DataValidateService {

    private final SYS_Data_Validate_RuleRepository sys_Data_Validate_RuleRepository;

    public DataValidateServiceImpl(SYS_Data_Validate_RuleRepository sys_Data_Validate_RuleRepository){

        this.sys_Data_Validate_RuleRepository=sys_Data_Validate_RuleRepository;
    }
    public R getValidateRecord(String wellName, String CaseSize)
    {
        Integer index=1;
        List<ValidateInfoOutputDto> lstResult=new ArrayList<ValidateInfoOutputDto>();
        String[] tablenames={"CM_WK_BASIC","CM_WK_MUD_PROPERTY","CM_WK_MUD_FORMULA"};

        for (String table:tablenames ) {
            List<SYS_DATA_VALIDATE_RULE> lstRule;
            lstRule= sys_Data_Validate_RuleRepository.findByTableName(table);
            for (SYS_DATA_VALIDATE_RULE rule:lstRule   ) {
                ValidateInfoOutputDto result=new  ValidateInfoOutputDto();
                result.setId(index.toString());
                result.setErrorInfo(rule.getDataRuleDecription());
                lstResult.add(result);
                index++;
            }
        }
        return R.ok().putData(lstResult);
    }


    public R getValidateRecord1(String wellName, String caseSize)  {
        String[] tablenames={"CM_WK_BASIC","CM_WK_MUD_PROPERTY","CM_WK_MUD_FORMULA"};
        List<ValidateInfoOutputDto> lstResult=new ArrayList<ValidateInfoOutputDto>();
        DBHelper dbHelper = new DBHelper();
        JSONObject jsonobj = null;
        Integer index=1;
        ValidateInfoOutputDto result = null;
        String sql = null;
        try {
        for (String name : tablenames) {

            if(name.equals("CM_WK_BASIC"))
            {
                sql = "SELECT * FROM " + name + " WHERE case_size = " + "\'" + caseSize + "\'" + " and wellname = " + "\'" + wellName + "\'";
            } else {
                sql = "SELECT * FROM " + name + " WHERE casingsize = " + "\'" + caseSize + "\'" + " and wellname = " + "\'" + wellName + "\'";

            }
            //拿出当前表中 井名为 wellName ,大小为 caseSize 的字段 以及 对应的信息
                jsonobj = dbHelper.execQuery(sql);
                //拿出需要校验的字段（根据表名）
                List<String> fieldName = sys_Data_Validate_RuleRepository.fieldName(name);
                if (fieldName.size() > 0) {
                    for (String s : fieldName) {
                        // 存放返回前端的数据
                       result=new  ValidateInfoOutputDto();
                        // 需要检验字段的信息
                        String str = jsonobj.getJSONArray("result").getJSONObject(0).getString(s);
                        //需要检验字段的校验的规则以及类型等信息
                        SYS_DATA_VALIDATE_RULE lstRule = sys_Data_Validate_RuleRepository.dataRuleByTableNameAndFiledName(name, s);
                        //如果是日期的类型，因为是格式原因需要进行格式化 并进行校验
                        String dataType = lstRule.getDataType();
                        //当前字段的规则描述
                        String decription = lstRule.getDataRuleDecription();
                        if (dataType.equals("DATE")) {
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = formatter.parse(str);
                            String dString = formatter.format(date);
                            System.out.println(dString);
//                        Pattern p = Pattern.compile(lstRule.getDataRule());
//                        Matcher m = p.matcher(dString);
//                        boolean dateFlag = m.matches();
                            boolean dateFlag = dString.matches(lstRule.getDataRule());
                            if (!dateFlag) {
                                result.setId(index.toString());
                                result.setErrorInfo(decription+"不正确");
                                index++;
                            } else {
//                            result.setId(index.toString());
//                            result.setErrorInfo(decription+" 正确无误");
//                            index++;
                            }
                        } else {
//                        Pattern p = Pattern.compile(lstRule.getDataRule());
//                        Matcher m = p.matcher(str);
//                        boolean dateFlag = m.matches();
                            boolean dateFlag = str.matches(lstRule.getDataRule());
                            if (!dateFlag) {
                                result.setId(index.toString());
                                result.setErrorInfo(decription+"不正确");
                                index++;
                            } else {
//                            result.setId(index.toString());
//                            result.setErrorInfo(decription+" 正确无误");
//                            index++;
                            }
                        }
                        if (result.getId() != null) {
                            lstResult.add(result);
                        }
                    }
                }else {
                    break;
                }
            }
        }catch (Exception e) {
            System.out.println("查询有问题");
        }
        return R.ok().putData(lstResult);
    }

}
